﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pokerist
{
    public partial class PokerPro : Form
    {
        List<Poker> PokerList;

        public PokerPro()
        {
            InitializeComponent();
            InitForm();
        }

        private void InitForm()
        {
            PokerList = new List<Poker>();
            
            cboPokerRole.SelectedIndex = 0;
            cboPosition.SelectedIndex = 0;
        }

        private void btnCreatePoker_Click(object sender, EventArgs e)
        {
            CreateNewPoker();
        }

        private void menuGoto_Opening(object sender, CancelEventArgs e)
        {
            if (lstPoker.SelectedIndex < 0)
                menuGoto.Items[0].Enabled = false;
            else
                menuGoto.Items[0].Enabled = true;
        }

        private void mnuItemActivatePoker_Click(object sender, EventArgs e)
        {
            tabPoker.SelectedTab = tabPoker.TabPages[lstPoker.SelectedValue.ToString()];
        }

        #region "Validation"
        private bool ValidateBeforeCreateNewPoker()
        {
            if (string.IsNullOrEmpty(txtPokerName.Text.Trim()))
            {
                MessageBox.Show("Poker name is required. Please enter the name.");                
                txtPokerName.Focus();
                return false;
            }
            else if (PokerList.Any(t => t.PokerName.ToUpper() == txtPokerName.Text.Trim().Replace(" ", "").ToUpper()))
            {
                MessageBox.Show("Poker name has existed. Please enter another name.");
                txtPokerName.SelectAll();
                txtPokerName.Focus();
                return false;
            }

            return true;
        }
        #endregion

        #region "Create/modify poker"
        private void CreateNewPoker()
        {
            if (!ValidateBeforeCreateNewPoker()) return;

            TabPage newTab;
            IntPtr handler;

            if (PokerList.Count < 1)
            {
                newTab = tabPoker.SelectedTab;                
                newTab.Name = txtPokerName.Text.Trim().Replace(" ", "");
                newTab.Text = txtPokerName.Text.Trim();

                CreateBrowser(ref newTab, out handler);
            }
            else
            {
                newTab = new TabPage
                {
                    Name = txtPokerName.Text.Trim().Replace(" ", ""),
                    Text = txtPokerName.Text.Trim(),
                };
                CreateBrowser(ref newTab, out handler);

                tabPoker.TabPages.Add(newTab);
            }

            var poker = new Poker
            {
                PokerName = newTab.Name,
                Caption = newTab.Text,
                Email = txtEmail.Text.Trim(),
                Password = txtPassword.Text.Trim(),
                Handler = handler
            };

            PokerList.Add(poker);

            lstPoker.DataSource = new BindingList<Poker>(PokerList);
            lstPoker.DisplayMember = "Caption";
            lstPoker.ValueMember = "PokerName";

            WebBrowser newBrowser = new WebBrowser();

        }

        private void CreateBrowser(ref TabPage tabPage, out IntPtr browserHandler)
        {
            WebBrowser newBrowser = tabPage.Controls.Cast<Control>().FirstOrDefault(t => t.GetType() == typeof(WebBrowser)) as WebBrowser;

            if (newBrowser == null)
            {
                newBrowser = new WebBrowser();                
                newBrowser.Url = new Uri("http://pokerist.com/en/sign-in/");
                newBrowser.Dock = DockStyle.Fill;
                tabPage.Controls.Add(newBrowser);
            }
            browserHandler = newBrowser.Handle;

            return;
        }
        #endregion

        private void btnRename_Click(object sender, EventArgs e)
        {
            var browser = tabPoker.SelectedTab.Controls.Cast<Control>().FirstOrDefault(t => t.GetType() == typeof(WebBrowser)) as WebBrowser;
            if (browser != null)
            {
                if (browser.Document != null)
                {
                    if (browser.Document.Cookie != null)
                    {
                        browser.Document.Cookie.Remove(0, browser.Document.Cookie.Length);
                    }
                }
                browser.Navigate(new Uri("http://pokerist.com/en/sign-in/"));
            }
        }
    }
}
